import UtilFuncs from '../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import GlobalConst from '../common/globalConst.js'
var global = GlobalConst.GlobalConst;

var Absorb2StraightConnlineSvc = {}
Absorb2StraightConnlineSvc.absorbMousePos2StraightConnLineBetweenCurentGraph = function (pCurentGraph, mousePoint, originOffsetPt,inDragProcessing) {
    let xabsorb = false;
    let yabsorb = false;
    let offsetPoint = { x: 0, y: 0 };
    let xMinGap = -1;
    let yMinGap = -1;

    let graphCnt = pCurentGraph.getZone().getCnt();

    let topoRes = pCurentGraph.getTopoRes();
    let nodeList = topoRes.getConnNodeList();
    for (let i = 0; i < nodeList.length; i++) {
        let node = nodeList[i];
        let connLineRes = node.getOnlyoneConnLine();
        if (connLineRes && connLineRes.getRefGraph()) {
            let connLineGraph = connLineRes.getRefGraph();
            let linePos = connLineGraph.getPosCopy();
            let headPt = linePos.getHead();
            let tailPt = linePos.getTail();
            let dis1 = funcs.distancePoint(graphCnt, headPt, 1);
            let dis2 = funcs.distancePoint(graphCnt, tailPt, 1);
            let curentMouseSelPt = headPt;
            let anotherPt = tailPt;
            if (dis2 < dis1) {
                curentMouseSelPt = tailPt;
                anotherPt = headPt;
            }

            let detax = anotherPt.x - curentMouseSelPt.x;
            let detay = anotherPt.y - curentMouseSelPt.y;
            let hornVertKind = funcs.chkHornVertKind(curentMouseSelPt, anotherPt);
            if (hornVertKind == 1) {
                //接近水平，则要把连接线拉水平，另一端anotherPt不动，本端curentMouseSelPt的Y值跟它对齐
                if (inDragProcessing && Math.abs(detay) < global.ZERO_4E){
                    continue;
                }
                
                if (yMinGap == -1) {
                    yabsorb = true;
                    yMinGap = Math.abs(detay);
                    offsetPoint.y = detay;
                }
                else if (Math.abs(detay) < yMinGap) {
                    yabsorb = true;
                    yMinGap = Math.abs(detay);
                    offsetPoint.y = detay;
                }
            }
            else if (hornVertKind == 2) {
                //接近水平，则要把连接线拉水平，另一端anotherPt不动，本端curentMouseSelPt的Y值跟它对齐
                if (inDragProcessing && Math.abs(detax) < global.ZERO_4E){
                    continue;
                }
                if (xMinGap == -1) {
                    xabsorb = true;
                    xMinGap = Math.abs(detax);
                    offsetPoint.x = detax;
                }
                else if (Math.abs(detax) < xMinGap) {
                    xMinGap = Math.abs(detax);
                    offsetPoint.x = detax;
                }
            }
        }
    }


    if (xabsorb || yabsorb) {
        return { offset: offsetPoint, xabsorb, yabsorb };
    }
    else {
        return null;
    }
}



export { Absorb2StraightConnlineSvc }